<?php
/*********************************************************************
 * CATEGORY CLASS FILE
 * 
 * Author: hthieu1110
 * Date: 18/04/2011
 **********************************************************************/
require_once dirname(realpath(__FILE__)).'./../config/config.inc.php';

class Category {
  private $categoryId;
  private $Db;
  private $isViaFetchObject;
  
  public function __construct($categoryId = '', $isViaFetchObject = false) {
    try {
      $this->Db = new Db();
    } catch(Exception $e) {
      throw new Exception('<br/>File : <b>'.__FILE__.'</b><br/>Line : <b>'.__LINE__.'</b><br/>Error : '.$e->getMessage());
    }
    
    $this->categoryId = $categoryId;
  }

#--------------------------------------------------------------------------------------
  public function getId() {
    if ($this->isViaFetchObject) {
      return $this->Id;
    } else {
      $query = sprintf("SELECT id FROM category WHERE id = '%d'",
                        mysql_escape_string($this->categoryId));
      
      $category = $this->Db->select($query, 'Category');

      return $category->Id;
    }
  }
#--------------------------------------------------------------------------------------
  public function getName() {
    $query = sprintf("SELECT name FROM category WHERE id = '%d'",
                      mysql_escape_string($this->categoryId));
                      
    try {
     	$categoryName = $this->Db->select($query);
    } catch (Exception $e) {
     	throw new Exception('<br />File : '.__FILE__.'<br />Line : '.__LINE__.'<br />Error : '.$e->getMessage());
    }
    
    return $categoryName['name'];
  }
  
#--------------------------------------------------------------------------------------
  public function isParentCategory() {
    //Special case: categoryId 0 ==> all category
    if ($this->categoryId == 0) return true;

    $query = sprintf("SELECT parentCategoryId FROM category WHERE id = '%d'",
                      mysql_escape_string($this->categoryId));
                      
    try {
      $parentCategoryId = $this->Db->select($query);
    } catch (Exception $e) {
     	throw new Exception('<br />File : '.__FILE__.'<br />Line : '.__LINE__.'<br />Error : '.$e->getMessage());
    }

    return ($parentCategoryId['parentCategoryId'] == 0);
  }
  
#--------------------------------------------------------------------------------------
  public function retrieveChildCategoryIds() {
    if (!$this->isParentCategory()) throw new Exception('<br />File : '.__FILE__.'<br />Line : '.__LINE__.'<br />Error : This category is not parent category. Verify it !!!');

    $query = sprintf("SELECT id FROM category WHERE parentCategoryId = '%d'",
                      mysql_escape_string($this->categoryId));

    //Special case: categoryId 0 ==> all category
    if ($this->categoryId == 0) $query = "SELECT id FROM category";
                      
    try {
      $childCategories = $this->Db->selectTable($query);

    } catch (Exception $e) {
     	throw new Exception('<br />File : '.__FILE__.'<br />Line : '.__LINE__.'<br />Error : '.$e->getMessage());
    }
    
    $childCategoryIds = array();
    foreach ($childCategories as $childCategory){
      $childCategoryIds[] = $childCategory['id'];
    }

    return (empty($childCategoryIds)) ? array($this->categoryId) : $childCategoryIds;
  }
}